package com.github.codemperor.core.swagger;

import io.swagger.models.Swagger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置类，默认prod环境不允许开启
 */
@ConditionalOnClass(value = {Swagger.class})
@Configuration
@EnableSwagger2
@Profile({"dev", "qa", "demo"})
public class SwaggerConfig {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${uptrade.swagger.basePackage:com.github.codemperor}")
    private String basePackage;

    @Bean
    public Docket createRestApi() {
        log.info("SwaggerConfig ~ applicationName:{} ~ basePackage: {} ~ init==========================", applicationName, basePackage);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName + " Api Document")
                .description(applicationName + " Api Document")
                .contact(new Contact("uptrade-us", "https://www.baidu.com", "codemperor@qq.com"))
                .version("1.0")
                .build();
    }
}
